package com.aha.dataanalyzeandview.service.impl;

import com.aha.dataanalyzeandview.config.ScheduledService;
import com.aha.dataanalyzeandview.entity.wxrobot.WxRobotSettingsEntity;
import com.aha.dataanalyzeandview.mapper.WxRobotMapper;
import com.aha.dataanalyzeandview.service.IWxRobotService;
import com.google.common.base.Joiner;
import lombok.AllArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;


@Service
public class WxRobotServiceImpl implements IWxRobotService {

    @Autowired
    private WxRobotMapper wxRobotMapper;

    @Autowired
    private ScheduledService scheduledService;


    @Override
    public List<WxRobotSettingsEntity> queryWxRobotSettingsInfo(Map<String, Object> map) {
        return wxRobotMapper.queryWxRobotSettingsInfo(map);
    }

    @Override
    public Integer addWxRobotSettingsInfo(WxRobotSettingsEntity wxRobotSettingsEntity) {
        return wxRobotMapper.addWxRobotSettingsInfo(wxRobotSettingsEntity);
    }

    @Override
    public Integer updateSendMsgRobotStatus(Map<String, Object> map) {

        int res = wxRobotMapper.updateSendMsgRobotStatus(map);
        // 关闭机器人
        if((Integer) map.get("status") == 0){
            scheduledService.closeWxRobotJobReset( Integer.valueOf(map.get("id").toString()));
        }else{
            // 添加机器人
            List<WxRobotSettingsEntity> wxRobotSettingsEntities = wxRobotMapper.queryWxRobotSettingsInfo(map);
            if(wxRobotSettingsEntities.size() == 1) {
                scheduledService.wxRobotJobReset(wxRobotSettingsEntities.get(0));
            }
        }

        return res;
    }


    @Override
    public Integer delRobotSettings(Integer id) {
        // 先关闭机器人
        scheduledService.closeWxRobotJobReset( id);

       int res =  wxRobotMapper.delRobotSettings(id);
        return res;
    }
}
